package service

import (
	"github.com/quarkcloudio/quark-go/v2/pkg/builder"
	"github.com/wsl/hi-go/model"
	captcha "github.com/wsl/hi-go/plugins/captcha"
	customTool "github.com/wsl/hi-go/plugins/customTool"
	passwordManager "github.com/wsl/hi-go/plugins/password"
	sqlManager "github.com/wsl/hi-go/plugins/sql"
	tokenManager "github.com/wsl/hi-go/plugins/token"
	"github.com/wsl/hi-go/types"
)

type User struct{}

// 登录
func (user *User) Login(c *builder.Context) error {
	type LoginParam struct {
		Username string `warning:"请输入用户名" json:"username"`
		Password string `warning:"请输入密码" json:"password"`
		CaptchId string `warning:"获取图形验证码ID失败" json:"captchId"`
		Captch   string `warning:"请输入图形验证码" json:"captch"`
	}
	var loginParam LoginParam
	c.Bind(&loginParam)
	// 检查参数是否合规，不合规提示默认提示语
	if _, err := customTool.CheckQuery(loginParam); err != nil {
		return c.JSONError(err.Error())
	}

	if _, err := (&captcha.Captcha{}).VerifyCaptcha(loginParam.CaptchId, loginParam.Captch); err != nil {
		return c.JSONError(err.Error())
	}

	errStr := "账号或密码错误！"
	if user, err := (&model.User{}).Query(loginParam.Username); err != nil {
		return c.JSONError(errStr)
	} else {
		if err := (&passwordManager.PasswordManager{}).VerifyIsEquality(loginParam.Password, user.Password); err != nil {
			return c.JSONError(errStr)
		}
		// 生成用户token
		tokenString := (&tokenManager.AdminClaims{}).CreateJWTToken(user.Id)
		return c.JSONOk("操作成功", map[string]interface{}{
			"token": tokenString,
			"name":  user.Name,
		})
	}
}

// 注册
func (user *User) Register(c *builder.Context) error {
	type RegisterParam struct {
		Username   string `warning:"请输入用户名" json:"username"`
		Password   string `warning:"请输入密码" json:"password"`
		RePassword string `warning:"请再次输入密码" json:"repassword"`
		CaptchId   string `warning:"无效的图形验证码ID" json:"captchId"`
		Captch     string `warning:"请输入图形验证码" json:"captch"`
	}
	var registerParam RegisterParam
	c.Bind(&registerParam)

	// 检查参数是否合规，不合规提示默认提示语
	if _, err := customTool.CheckQuery(registerParam); err != nil {
		return c.JSONError(err.Error())
	}

	// 附加判断
	if _, err := customTool.StringIsEquality(registerParam.Password, registerParam.RePassword, "两次输入的密码不一致"); err != nil {
		return c.JSONError(err.Error())
	}

	if _, err := (&model.User{}).Query(registerParam.Username); err == nil {
		return c.JSONError("用户已存在")
	}

	if _, err := (&captcha.Captcha{}).VerifyCaptcha(registerParam.CaptchId, registerParam.Captch); err != nil {
		return c.JSONError(err.Error())
	}

	// 密码加密
	if password, err := (&passwordManager.PasswordManager{}).TransformStringToPassword(registerParam.Password); err != nil {
		return c.JSONError(err.Error())
	} else {
		if err := (&model.User{}).Create(registerParam.Username, password); err != nil {
			return c.JSONError(err.Error())
		}
	}
	return c.JSONOk("操作成功", map[string]interface{}{})
}

// 用户列表
func (user *User) UserIndex(c *builder.Context) error {
	page, pageSize := sqlManager.PageManagerGetQuery(c)
	result, err := (&model.User{}).Index(page, pageSize)
	if err != nil {
		return c.JSONError(err.Error())
	} else {
		return c.JSONOk("操作成功", map[string]interface{}{
			"list":  result.List,
			"total": result.Total,
		})
	}
}

// 用户删除
func (user *User) UserDelete(c *builder.Context) error {
	id := sqlManager.TransFormInt(c.Query("id"), 0, func(i int) bool { return i > 0 })
	if id == 0 {
		return c.JSONError("无效的用户ID")
	}
	err := (&model.User{}).Delete(id)
	if err != nil {
		return c.JSONError(err.Error())
	} else {
		return c.JSONOk("操作成功")
	}
}

// 用户更新
func (user *User) UserUpdate(c *builder.Context) error {
	var updateParam types.UpdateParam
	c.Bind(&updateParam)
	id := sqlManager.TransFormInt(updateParam.Id, 0, func(i int) bool { return i > 0 })
	if id == 0 {
		return c.JSONError("无效的用户ID")
	}
	err := (&model.User{}).Update(updateParam)
	if err != nil {
		return c.JSONError(err.Error())
	} else {
		return c.JSONOk("操作成功")
	}
}

// 用户查询
func (user *User) UserDetail(c *builder.Context) error {
	id := sqlManager.TransFormInt(c.Query("id"), 0, func(i int) bool { return i > 0 })
	if id == 0 {
		return c.JSONError("无效的用户ID")
	}
	userResult, err := (&model.User{}).Detail(id)
	if err != nil {
		return c.JSONError(err.Error())
	} else {
		return c.JSONOk("操作成功", map[string]interface{}{
			"user": userResult,
		})
	}
}
